package com.ruoyi.jihuo.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jihuo.domain.JhTmProductsOnSale;
import com.ruoyi.jihuo.service.IJhTmProductsOnSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 在售商品Controller
 *
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/jihuo/sale")
public class JhTmProductsOnSaleController extends BaseController {
    @Autowired
    private IJhTmProductsOnSaleService jhTmProductsOnSaleService;

    /**
     * 查询在售商品列表
     */
    @PreAuthorize("@ss.hasPermi('jihuo:sale:list')")
    @GetMapping("/list")
    public TableDataInfo list(JhTmProductsOnSale jhTmProductsOnSale) {
        startPage();
        List<JhTmProductsOnSale> list = jhTmProductsOnSaleService.selectJhTmProductsOnSaleList(jhTmProductsOnSale);
        return getDataTable(list);
    }

    /**
     * 导出在售商品列表
     */
    @PreAuthorize("@ss.hasPermi('jihuo:sale:export')")
    @Log(title = "在售商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JhTmProductsOnSale jhTmProductsOnSale) {
        List<JhTmProductsOnSale> list = jhTmProductsOnSaleService.selectJhTmProductsOnSaleList(jhTmProductsOnSale);
        ExcelUtil<JhTmProductsOnSale> util = new ExcelUtil<JhTmProductsOnSale>(JhTmProductsOnSale.class);
        util.exportExcel(response, list, "在售商品数据");
    }

    /**
     * 获取在售商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('jihuo:sale:query')")
    @GetMapping(value = "/{jhId}")
    public AjaxResult getInfo(@PathVariable("jhId") Long jhId) {
        return success(jhTmProductsOnSaleService.selectJhTmProductsOnSaleByJhId(jhId));
    }

    /**
     * 新增在售商品
     */
    @PreAuthorize("@ss.hasPermi('jihuo:sale:add')")
    @Log(title = "在售商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JhTmProductsOnSale jhTmProductsOnSale) {
        return toAjax(jhTmProductsOnSaleService.insertJhTmProductsOnSale(jhTmProductsOnSale));
    }

    /**
     * 修改在售商品
     */
    @PreAuthorize("@ss.hasPermi('jihuo:sale:edit')")
    @Log(title = "在售商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JhTmProductsOnSale jhTmProductsOnSale) {
        return toAjax(jhTmProductsOnSaleService.updateJhTmProductsOnSale(jhTmProductsOnSale));
    }

    /**
     * 删除在售商品
     */
    @PreAuthorize("@ss.hasPermi('jihuo:sale:remove')")
    @Log(title = "在售商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jhIds}")
    public AjaxResult remove(@PathVariable Long[] jhIds) {
        return toAjax(jhTmProductsOnSaleService.deleteJhTmProductsOnSaleByJhIds(jhIds));
    }

    /**
     * 清理无流量 ? 几天内
     *  todo 暂定7天
     */
    @PreAuthorize("@ss.hasPermi('jihuo:sale:remove')")
    @Log(title = "在售商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/clear")
    public AjaxResult clear() {
        return toAjax(jhTmProductsOnSaleService.clear());
    }
}
